<template>
  <div class="layout">
    <div class="form">
      <p>电子邮件</p>
      <input class="user" type="text" v-model="userVal" />
      <p>密码</p>
      <input class="pass" type="password" v-model="passVal" />
      <button @click="signin">登录</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      userVal: "",
      passVal: "",
      isCheck: false,
    };
  },
  methods: {
    signin() {
      // 用户登录
      axios
        .post(
          "/login",
          { email: this.userVal, password: this.passVal },
          { emulateJSON: true }
        )
        // .get("/login", {
        //   params: { email: this.userVal, password: this.passVal }
        // })
        .then((data) => {
          if (data.code == 200) {
            this.isCheck = true;
            if (this.isCheck) {
              localStorage.setItem("token", data.token);
              localStorage.setItem("user", data.profile.nickname);
              this.setCookie("user", data.userId);
              this.$router.push({ path: "/home" });
            } else {
              console.log("账号或密码错误！");
            }
          }
        });
    },
    setCookie(key, value) {
      document.cookie = key + "=" + value + ";";
    },
  },
};
</script>

<style lang="scss" scoped>
.layout {
  width: 100%;
  height: 100%;
  background-color: #121212;
  color: white;
  overflow: hidden;
  .form {
    text-align: center;
    margin-top: 100px;
    padding: 0 15px;
    p {
      text-align: left;
      margin-bottom: 5px;
    }
    input {
      width: 100%;
      height: 38px;
      border-radius: 5px;
      margin-bottom: 20px;
      padding-left: 15px;
      border: 0;
      color: white;
      background-color: #414141;
    }
    button {
      width: 80px;
      height: 38px;
      border-radius: 20px;
      border: 0;
      // background-color: #4D4D4D;
      background-color: #fff;
      font-weight: bold;
    }
  }
}
</style>
